class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int slow = 0, fast = 0;
        do // 慢指针每次走一步，快指针每次走两步，两个指针在有环的情况下一定会相遇
        {
            slow = nums[slow];
            fast = nums[nums[fast]];
        } while (slow != fast);
        slow = 0; // 将 slow 放置起点 0，两个指针每次同时移动一步，相遇的点就是答案
        while (slow != fast)
        {
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }
};